var network = require("./network.js")
var util = require("./util.js")
var retryCount = 0

function recognize(options) {
    var url = options.url,
        path = options.path,
        data = options.data,
        callback = options.callback,
        fail = options.fail

    // wx.showLoading({
    //     title: '正在识别...',
    //     // mask:true
    // })
    network.post({
        url: url,
        data: {
            path: path
        },
        extra: data,
        success: function (data) {
            if (callback) {
                callback(data)
            }
            // wx.hideLoading()
        },
        fail: function (res) {
            if (res.data.code == -1308 && retryCount == 0) {
                console.log('-1308:retry')
                retryCount++
                recognize(options)
            }
            else {
                if (retryCount) {
                    retryCount = 0
                }
                // wx.hideLoading()
                if(fail) {
                    fail(res)
                }
            }
        }
    })
}

function upload(options) {
    // wx.showLoading({
    //     title: '正在识别...',
    //     // mask:true
    // })
    var url = options.url,
        data = options.data,
        src = options.src,
        callback = options.callback,
        fail = options.fail

    network.upload({
        url: url,
        path: src,
        name: 'file',
        extra: data,
        success: function (res) {
            var path = res.data
            if (callback) {
                callback(path)
            }
        },
        fail: function (res) {
            // wx.hideLoading()
            if (fail) {
                fail(res)
            }
        }
    })
}

module.exports = {
    recognize: recognize,
    upload: upload
}